package cn.flaty.utils.web.file;

import java.util.ArrayList;
import java.util.List;

import cn.flaty.utils.web.file.UploadInfo;
import cn.flaty.utils.web.file.UploadResult;

/**
 * 文件写入抽像类模板类
 * 
 * @author flatychen
 * @date 2014-4-21
 */
public abstract class AbstractFileUpload extends FileValidChecker {
	
	 public String getRelativeUploadPath() {
		return relativeUploadPath;
	}

	public void setRelativeUploadPath(String relativeUploadPath) {
		this.relativeUploadPath = relativeUploadPath;
	}

	private String relativeUploadPath;

	public final UploadResult uploadFile(UploadInfo file) {

		this.setFileInfo(file);
		// 检查文件上传是否合法,主要为文件大小与文件类型
		UploadResult upt = new UploadResult();
		if (!checkFileSize()) {
			upt.setSuccess(false);
			upt.setMessage(checkFileSize_MESSAGE);
			return upt;
		}
		if (!checkContentType()) {
			upt.setSuccess(false);
			upt.setMessage(checkContentType_MESSAGE);
			return upt;
		}

		return writeFile(file);

	};

	public final List<UploadResult> uploadFiles(UploadInfo files[]) {
		List<UploadResult> uploadResults = new ArrayList<UploadResult>();
		for (int i = 0; i < files.length; i++) {
			UploadResult uploadResult = uploadFile(files[i]);
			if (uploadResult != null) {
				uploadResults.add(uploadResult);
			}
		}
		return uploadResults;
	}

	/**
	 * 写入文件，交由子类实现
	 * 
	 * @author flatychen
	 * @date 2014-4-29
	 * @return
	 */
	protected abstract UploadResult writeFile(UploadInfo fileInfo);

}
